package sg.edu.nus.iss.vmcs.entity.simuator;

import java.io.IOException;

import sg.edu.nus.iss.vmcs.entity.Brand;
import sg.edu.nus.iss.vmcs.entity.BrandDrink;
import sg.edu.nus.iss.vmcs.entity.BrandDrinkStore;
import sg.edu.nus.iss.vmcs.entity.BrandDrinkStoreItem;
import sg.edu.nus.iss.vmcs.entity.CashStore;
import sg.edu.nus.iss.vmcs.entity.CashStore1;
import sg.edu.nus.iss.vmcs.entity.CashStoreItem;
import sg.edu.nus.iss.vmcs.entity.Coin;
import sg.edu.nus.iss.vmcs.entity.StoreItem;
import sg.edu.nus.iss.vmcs.entity.simuator.dao.ISimuatorCashDAO;
import sg.edu.nus.iss.vmcs.entity.simuator.dao.ISimulatorDrinkBrandDAO;
import sg.edu.nus.iss.vmcs.entity.simuator.dao.impl.SimulatorCashDAOImpl;
import sg.edu.nus.iss.vmcs.entity.simuator.dao.impl.SimulatorDrinkBrandDAOImpl;

public class SimulatorCashStore extends CashStore1 {

	private String CASH_PROPERTIES = "source/CashPropertyFile.txt";
	private static SimulatorCashStore simulatorCashStore = null;
	private ISimuatorCashDAO iSimulatorCashDAO = null;
	
	private static final String NAME_LABEL     = "Name";
	private static final String WEIGHT_LABEL   = "Weight";
	private static final String VALUE_LABEL    = "Value";
	private static final String THICKNESS_LABEL = "Thickness";
	private static final String TAILIMG_LABEL = "Tailimg";
	private static final String RADIUS_LABEL = "Radius";
	private static final String HEADIMG_LABEL = "Headimg";
	private static final String QUANTITY_LABEL = "Quantity";
	private static final String INVALID_COIN = "Invalid Coin";
	
	
	private SimulatorCashStore(){
		try {
			iSimulatorCashDAO = SimulatorCashDAOImpl.getSimulatorCashDAO(CASH_PROPERTIES);
			processStore();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override
	protected void processStore() {
		int noOfItems = iSimulatorCashDAO.getNumOfItems();
		this.setStoreSize(noOfItems);

		for(int i=1;i<=noOfItems;i++){
			try{
				String name = iSimulatorCashDAO.getAttributeValue(NAME_LABEL + i);
				String weight = iSimulatorCashDAO.getAttributeValue(WEIGHT_LABEL + i);
				String value = iSimulatorCashDAO.getAttributeValue(VALUE_LABEL + i);
				String quantity = iSimulatorCashDAO.getAttributeValue(QUANTITY_LABEL + i);
				
				String thickness = iSimulatorCashDAO.getAttributeValue(THICKNESS_LABEL + i);
				String tailImg   = iSimulatorCashDAO.getAttributeValue(TAILIMG_LABEL + i);
				String headImg   = iSimulatorCashDAO.getAttributeValue(HEADIMG_LABEL + i);
				String radius    = iSimulatorCashDAO.getAttributeValue(RADIUS_LABEL + i);
				
				Coin coin = new Coin();
				coin.setId(""+i);
				coin.setName(name);
				coin.setWeight(Double.parseDouble(weight));
				coin.setValue(Double.parseDouble(value));
				coin.setHeadImg(headImg);
				coin.setRadius(Double.parseDouble(radius));
				coin.setTailImg(tailImg);
				coin.setThickness(Double.parseDouble(thickness));
				
				if(INVALID_COIN.equals(coin.getValue()))
						coin.setId("-1");
				int qty = Integer.parseInt(quantity);
				CashStoreItem item = new CashStoreItem(coin, qty);
				addItem((i-1), item);
			}catch(Exception e){
				System.out.println("Error at CashStore  "+e.getMessage());
			}
		}
	}

	public static CashStore1 getInstance() {
		if(simulatorCashStore == null){
			simulatorCashStore = new SimulatorCashStore();
		}
		return simulatorCashStore;
	}
	
	public void updateItem(StoreItem object){
		super.updateItem(object);
		storeRecords();
	}
	
	public void storeRecords(){
		for (int i = 0; i < size; i++) {
			if (items[i] == null || items[i].getContent() == null)
				continue;
			StoreItem storeItem = items[i];
			Coin coin = (Coin) storeItem.getContent();
			int quantity = storeItem.getQuantity();
			String id = coin.getId();
			String value = ""+coin.getValue();
			String name = coin.getName();
			String weight = ""+coin.getWeight();
			String KEY_NAME = NAME_LABEL+id;
			String KEY_VALUE = VALUE_LABEL+id;
			String KEY_WEIGHT = WEIGHT_LABEL+id;
			String KEY_QUANTITY = QUANTITY_LABEL+id;
			
			String KEY_THICKNESS = THICKNESS_LABEL+id;
			String KEY_TAILIMG = TAILIMG_LABEL+id;
			String KEY_HEADIMG = HEADIMG_LABEL+id;
			String KEY_RADIUS = RADIUS_LABEL+id;
			
			try {
				iSimulatorCashDAO.update(KEY_NAME,name);
				iSimulatorCashDAO.update(KEY_VALUE,value);
				iSimulatorCashDAO.update(KEY_WEIGHT,weight);
				iSimulatorCashDAO.update(KEY_QUANTITY,""+quantity);
				
				iSimulatorCashDAO.update(KEY_THICKNESS,""+coin.getThickness());
				iSimulatorCashDAO.update(KEY_TAILIMG,""+coin.getTailImg());
				iSimulatorCashDAO.update(KEY_HEADIMG,""+coin.getHeadImg());
				iSimulatorCashDAO.update(KEY_RADIUS,""+coin.getRadius());
				
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}            
		}
	}
}
